package com.bs.bszhsq.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.bszhsq.entity.Kehu;
import com.bs.bszhsq.exception.ServiceException;
import com.bs.bszhsq.service.KehuService;
import com.bs.bszhsq.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("/kehu")
@Api(tags = "客户管理相关接口")
@Slf4j
public class KehuController {
    @Resource
    private KehuService kehuService;

    @GetMapping("/page")
    @ApiOperation(value = "客户分页查询")
    public R page(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                  @RequestParam(defaultValue = "") String xingming) {
        IPage<Kehu> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Kehu> qw = new QueryWrapper<>();
        if (!"".equals(xingming)) {
            qw.like("xingming", xingming);
        }
        qw.orderByDesc("id");
        IPage<Kehu> kehuIPage = kehuService.page(page, qw);
        return R.success(kehuIPage);
    }

    @PostMapping
    @ApiOperation(value = "新增客户")
    public R save(@RequestBody Kehu kehu) {
        if (BeanUtil.isEmpty(kehu)){
            throw new ServiceException("500","传入参数不完整！");
        }
        kehu.setAddtime(new Date());
        kehu.setZhaopian("http://localhost:8080/springboot224bf/upload/kehu_zhaopian4.jpg");
        boolean save = kehuService.save(kehu);
        return R.write(save);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改客户信息")
    public R update(@RequestBody Kehu kehu) {
        boolean update = kehuService.updateById(kehu);
        return R.write(update);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除客户")
    public R delete(@RequestBody Long id) {
        boolean b = kehuService.removeById(id);
        return R.write(b);
    }

    @PostMapping("/deletebatch")
    @ApiOperation(value = "批量删除")
    public R deletebatch(@RequestBody List<Long> ids) {
        boolean b = kehuService.removeByIds(ids);
        return R.write(b);
    }
@GetMapping("/export")
@ApiOperation(value = "客户信息导出")
    public R export(HttpServletResponse response) throws Exception {
        //先获取全部数据
    List<Kehu> list = kehuService.list();
    //写出到浏览器
    ExcelWriter writer = ExcelUtil.getWriter(true);
  //定义别名
    writer.addHeaderAlias("id","ID");
    writer.addHeaderAlias("xingming","姓名");
    writer.addHeaderAlias("xingbie","性别");
    writer.addHeaderAlias("nianling","年龄");
    writer.addHeaderAlias("shouji","手机号");
    writer.addHeaderAlias("youxiang","邮箱");
    writer.addHeaderAlias("jiatingzhuzhi","家庭地址");
    writer.addHeaderAlias("jiaofeiqingkuang","缴费情况");
    writer.addHeaderAlias("fangwuxinxi","房屋信息");
    writer.addHeaderAlias("fangchanziliao","房产资料");
    writer.addHeaderAlias("addtime","创建时间");
    writer.addHeaderAlias("zhaopian","照片");

    //写入数据
    writer.write(list,true);
    //设置浏览器响应格式
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    String fileName = URLEncoder.encode("客户信息", "UTF-8");
//test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
    response.setHeader("Content-Disposition","attachment;filename="+fileName+".xls");
    ServletOutputStream outputStream = response.getOutputStream();
    writer.flush(outputStream,true);
    outputStream.close();
    writer.close();
    return R.success();
}
@PostMapping("/import")
    @ApiOperation(value = "导入客户信息")
public R imp(MultipartFile file) throws Exception{
    InputStream inputStream = file.getInputStream();
    ExcelReader reader = ExcelUtil.getReader(inputStream);
    Map<String,String> map=new HashMap<>();
    map.put("姓名","xingming");
    map.put("性别","xingbie");
    map.put("年龄","nianling");
    map.put("手机号","shouji");
    map.put("邮箱","youxiang");
    map.put("家庭地址","jiatingzhuzhi");
    map.put("缴费情况","jiaofeiqingkuang");
    map.put("房屋信息","fangwuxinxi");
    map.put("房产资料","fangchanziliao");
    map.put("照片","zhaopian");
    reader.setHeaderAlias(map);
    List<Kehu> kehus = reader.readAll(Kehu.class);
    for (Kehu kehu : kehus) {
        kehu.setAddtime(new Date());
    }
    boolean b = kehuService.saveBatch(kehus);
    System.out.println("文件导入"+b);
    return R.write(b);
}
}
